package cn.lizemin.fakedata.security;

import cn.lizemin.fakedata.security.entity.Role;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import lombok.Data;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author lzm
 * @Date 2024/7/7
 * @description
 */
@Data
public class User implements UserDetails {

    private Integer id;

    private String username;

    @JsonIgnore
    private String password;

    private List<Role> roles;

    @Override
    public List<String> getAuthorities() {
        if (CollectionUtils.isEmpty(roles)) {
            return new ArrayList<>();
        }
        // 从数据库中根据用户的角色查询出用户拥有的所有权限
        List<Integer> ids = roles.stream().map(Role::getId).collect(Collectors.toList());
        if (ids.contains(2)) {
            return Lists.newArrayList("order.create");
        } else {
            return Lists.newArrayList("*");
        }
    }

}
